<?php
/**
 * Main View class
 */
class Main_View extends Psa_Plugin_View{
	
	
	/**
	 * Login page.
	 */
	function login(){
		
		// authorization error
		if(isset($this->psa_result->unsuccessful_authorize)){
			$this->psa_smarty->assign('error', 'You entered invalid username or password.');
		}
		
		$this->psa_smarty->assign('header_main', $this->psa_smarty->fetch("header_main.tpl"));
		$this->psa_smarty->assign('content_main', $this->psa_smarty->fetch("login.tpl"));
	}
	
	
	/**
	 * Main template render and display method.
	 * This method determine if request is made by ajax or not and echo only
	 * ajax result or whole html page.
	 */
	function generate_html(){
		
		// if this is ajax request just echo content
		if(@$this->psa_result->ajax_request){
			header ("Content-Type: text/html; charset=utf-8");
			if(isset($this->psa_result->ajax_response))
				echo $this->psa_result->ajax_response;
			else
				echo @$this->psa_smarty->_tpl_vars['content'];
		}
		// render complete page
		else{
			
			global $CFG, $PSA_CFG;
			
			$include_js = $include_css = array();
		
			// aggregate js and css files
			if(@$CFG['js_files_aggregate'])
				$include_js[]  = 'js/aggregate_'  . filemtime_array($CFG['js_files_aggregate'])  . '.js';
			if(@$CFG['css_files_aggregate'])
				$include_css[] = 'css/aggregate_' . filemtime_array($CFG['css_files_aggregate']) . '.css';
			
			// js and css files that must not be aggregated
			if(@$CFG['js_files'])
				$include_js = array_merge($include_js, $CFG['js_files']);
			if(@$CFG['css_files'])
				$include_css = array_merge($include_css, $CFG['css_files']);
			
			if(@$include_js)
				$this->psa_smarty->assign('include_js', $include_js);
			if(@$include_css)
				$this->psa_smarty->assign('include_css', $include_css);
			
			
			// asign page header and main menu
			$this->psa_smarty->assign('header_main', $this->psa_smarty->fetch('header_main.tpl'));
			$this->psa_smarty->assign('layout_left', $this->psa_smarty->fetch('menu_main.tpl'));
			
			// only in develop mode show PSA request info console
			if($PSA_CFG['develop_mode'])
				$this->psa_smarty->assign('psa_request_info', psa_request_info_html());
			
			// html page main content
			if(!isset($this->psa_smarty->_tpl_vars['content_main'])){
				$this->psa_smarty->assign('content_main', $this->psa_smarty->fetch('layout_main.tpl'));
			}
			
			// echo complete rendered page
			echo $this->psa_smarty->fetch('main.tpl');
		}
	}
	
	
	/**
	 * On uncaught exception
	 * @param exception $e
	 */
	function uncaught_exception_redirect($e){
		
		$exception['code'] = $e->getCode();
		$exception['msg'] = $e->getMessage();
		$exception['type'] = get_class($e);
		
		// if this is ajax request
		if(@$this->psa_result->ajax_request){
			
			// send 500 header
			if(!headers_sent())
				header('HTTP/1.1 500 Internal Server Error');
			
			$this->psa_smarty->assign("exception", $exception);
			echo $this->psa_smarty->fetch("exception_ajax.tpl");
		}
		else{
			global $PSA_CFG;
			if(!headers_sent()){
				header("Location: {$PSA_CFG['folders']['basedir_web']}/exception/" . base64_encode($exception['type'] . '#.#' . $exception['msg'] . '#.#' . $exception['code']));
			}
			else{
				$this->psa_smarty->assign("exception", $exception);
				echo $this->psa_smarty->fetch("exception.tpl");
			}
		}
		
		exit;
	}
	
	
	/**
	 * Uncaught exception error page
	 * @param string $exception_data
	 */
	function uncaught_exception($exception_data){

		if($exception_data){
			$exception_data = explode('#.#', base64_decode($exception_data));
			$exception['code'] = $exception_data[2];
			$exception['msg'] = $exception_data[1];
			$exception['type'] = $exception_data[0];
			$this->psa_smarty->assign("exception", $exception);
		}

		$this->psa_smarty->assign('content', $this->psa_smarty->fetch('exception.tpl'));
	}	
}
